﻿using NUnit.Framework;

namespace Agile.Crm4.Plugins.FilteredLookup.Tests
{
    public class LookupMultiTests : FilterLookupTests
    {
        [Test]
        public void Parse_PageUrlWithFilterThatMatchesEntityName_ShouldReturnMatchingFilter()
        {
            var pageReferer = string.Empty;
            var pageUrl = "http://CrmService/OrganizationName/_controls/lookup/_controls/lookup/lookupmulti.aspx?class=ActivityRecipient&objecttypes=1,2,4,8,2020&browse=0&ShowNewButton=1&ShowPropButton=1&DefaultType=0&filters=" + Filters;
            var adapter = new AdditionalFilterAdapter(Key, EntityName, pageUrl, pageReferer);

            var actual = adapter.GetAdditionalFilter();
            var expected = "<filter><condition attribute=\"firstname\" operator=\"like\" value=\"moti\" /></filter>";

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void Parse_ForValidFetchXml_EntityNameShouldBeParsed()
        {
            var fetchXml = "<fetch version=\"1.0\" page=\"1\" count=\"100\" output-format=\"xml-platform\" mapping=\"logical\"><entity name=\"contact\"><attribute name=\"contactid\" />" +
                            "<attribute name=\"fullname\" />" +
                            "<attribute name=\"telephone1\" />" +
                            "<attribute name=\"fax\" />" +
                            "<attribute name=\"address1_fax\" />" +
                            "<attribute name=\"address1_name\" />" +
                            "<order attribute=\"fullname\" />" +
                            "<filter type=\"and\">" +
                            "<condition attribute=\"statecode\" operator=\"ne\" value=\"1\" />" +
                            "<filter type=\"or\">" +
                            "<condition attribute=\"fullname\" operator=\"like\" value=\"%\" />" +
                            "<condition attribute=\"firstname\" operator=\"like\" value=\"%\" />" +
                            "<condition attribute=\"lastname\" operator=\"like\" value=\"%\" />" +
                            "<condition attribute=\"middlename\" operator=\"like\" value=\"%\" />" +
                            "<condition attribute=\"emailaddress1\" operator=\"like\" value=\"%\" />" +
                            "</filter>" +
                            "</filter>" +
                            "</entity>" +
                            "</fetch>";

            var fetchXmlAdapter = new FetchXmlAdapter(fetchXml);
            Assert.AreEqual("contact", fetchXmlAdapter.EntityName);
        }

        [Test]
        public void Parse_WhenInjectingFilter_NewFetchXmlShouldContainTheFilter()
        {
            var fetchXml = "<fetch version=\"1.0\" page=\"1\" count=\"100\" output-format=\"xml-platform\" mapping=\"logical\"><entity name=\"contact\"><attribute name=\"contactid\" />" +
                            "<attribute name=\"fullname\" />" +
                            "<attribute name=\"telephone1\" />" +
                            "<attribute name=\"fax\" />" +
                            "<attribute name=\"address1_fax\" />" +
                            "<attribute name=\"address1_name\" />" +
                            "<order attribute=\"fullname\" />" +
                            "<filter type=\"and\">" +
                            "<condition attribute=\"statecode\" operator=\"ne\" value=\"1\" />" +
                            "<filter type=\"or\">" +
                            "<condition attribute=\"fullname\" operator=\"like\" value=\"%\" />" +
                            "<condition attribute=\"firstname\" operator=\"like\" value=\"%\" />" +
                            "<condition attribute=\"lastname\" operator=\"like\" value=\"%\" />" +
                            "<condition attribute=\"middlename\" operator=\"like\" value=\"%\" />" +
                            "<condition attribute=\"emailaddress1\" operator=\"like\" value=\"%\" />" +
                            "</filter>" +
                            "</filter>" +
                            "</entity>" +
                            "</fetch>";

            var fetchXmlAdapter = new FetchXmlAdapter(fetchXml);

            var filter = "<filter><condition attribute=\"contactid\" operator=\"eq\" value=\"{B658B921-6C27-45e1-B6AE-0B2724BFB588}\"/></filter>";
            fetchXmlAdapter.InjectFilter(filter);

            var expected = "<fetch version=\"1.0\" page=\"1\" count=\"100\" output-format=\"xml-platform\" mapping=\"logical\"><entity name=\"contact\"><attribute name=\"contactid\" />" +
                         "<attribute name=\"fullname\" />" +
                         "<attribute name=\"telephone1\" />" +
                         "<attribute name=\"fax\" />" +
                         "<attribute name=\"address1_fax\" />" +
                         "<attribute name=\"address1_name\" />" +
                         "<order attribute=\"fullname\" />" +
                         "<filter type=\"and\">" +
                         filter +
                         "<filter type=\"and\">" +
                         "<condition attribute=\"statecode\" operator=\"ne\" value=\"1\" />" +
                         "<filter type=\"or\">" +
                         "<condition attribute=\"fullname\" operator=\"like\" value=\"%\" />" +
                         "<condition attribute=\"firstname\" operator=\"like\" value=\"%\" />" +
                         "<condition attribute=\"lastname\" operator=\"like\" value=\"%\" />" +
                         "<condition attribute=\"middlename\" operator=\"like\" value=\"%\" />" +
                         "<condition attribute=\"emailaddress1\" operator=\"like\" value=\"%\" />" +
                         "</filter>" +
                         "</filter>" +
                         "</filter>" +
                         "</entity>" +
                         "</fetch>";

            expected = expected.Replace(" ", "");

            var actual = fetchXmlAdapter.FetchXml.Replace(" ", ""); ;

            Assert.AreEqual(expected, actual);

        }
    }
}
